package homework.day05;

import java.io.*;

/**
 * 将当前目录下的所有obj文件获取到，并进行
 * 反序列化后输出每个用户的信息(直接输出反序
 * 列化后的User对象即可)
 * @author Xiloer
 *
 */
public class Test04 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        File file = new File(".");
            FileFilter filter = pathname -> pathname.getName().endsWith(".obj");
            File[] fn = file.listFiles(filter);
            for (int i=0;i<fn.length;i++){
                try (ObjectInputStream ois =
                             new ObjectInputStream(
                                     new FileInputStream(fn[i].getName())
                             )                               //try()括号内按此格式不必写close()
                ){
                    Object u = ois.readObject();
                    if (u instanceof User){
                        User u1 = (User) u;
                        System.out.println(u1);
                        System.out.println("写出成功!");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
//        FileInputStream fis = new FileInputStream("user.obj");
//        ObjectInputStream ois = new ObjectInputStream(fis);
//        User u;
//        u = (User)ois.readObject();
//        System.out.println(u);
//        System.out.println("写出成功!");
//        ois.close();
    }
}
